﻿using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiMinCRMPlugin
{
    /// <summary>
    /// 应收票据操作插件
    /// </summary>
    /// <remarks>
    /// 功能：应收票据在提交时判断上游是否关联
    /// 收款清单，根据待认领收款的是否15加银行、是否关联票据更新应收票据对应字段。
    /// 创建人：wzx
    /// 创建日期：2024.7.9
    /// 修改信息（1）：修改人，修改日期，修改内容
    /// </remarks>
    /// 
    [Description("应收票据提交查询待认领收款")]
    [HotUpdate]
    public class BillReceiveableSearchPendingCollectionList : AbstractOperationServicePlugIn
    {
        //OnPreparePropertys 数据加载前，确保需要的属性被加载
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            //编码
            e.FieldKeys.Add("FBillNo");

            //数据来源
            e.FieldKeys.Add("F_DEV_DataSrc");

        }
        /// <summary>
        /// 事务结束后事件
        /// </summary>
        /// <param name="e"></param>
        public override void EndOperationTransaction(EndOperationTransactionArgs e)
        {
            base.EndOperationTransaction(e);

            //读取全部的单据,for循环,转换成DynamicObject类型
            foreach (DynamicObject entity in e.DataEntitys)
            {
                string DataSrc = Convert.ToString(entity["F_DEV_DataSrc"]);//数据来源
                string FID = Convert.ToString(entity["Id"]);//内码                      
                string Number = entity["BillNo"].ToString();//编码
                if (DataSrc == "CRM")
                {
                    //查询关联信息
                    string LK = string.Format(@"/*dialect*/  select FSBILLID,FSID from T_CN_BILLRECEIVABLE_LK where FRULEID='RDLV_ClaimListToBillReceivableNew' 
and FSTABLENAME='RDLV_t_PendingCollection' and FID='{0}'", FID);
                    DataTable dtLK = DBServiceHelper.ExecuteDataSet(this.Context, LK).Tables[0];
                    if (dtLK.Rows.Count > 0)
                    {
                        foreach (DataRow item in dtLK.Rows)
                        {
                            string PeID = Convert.ToString(item["FSBILLID"]);//待认领收款FID
                            //查询上游待认领收款的是否15加银行、是否关联票据
                            string sqlPCL = string.Format(@"/*dialect*/  select F_QLTK_15J,F_QLTK_GLF from RDLV_t_PendingCollection where FID='{0}'", PeID);
                            DataTable dtPCL = DBServiceHelper.ExecuteDataSet(this.Context, sqlPCL).Tables[0];
                            string F_QLTK_15J = ""; //是否15加银行
                            string F_QLTK_GLF = "";//是否关联票据
                            if (dtPCL.Rows.Count>0)
                            {
                                F_QLTK_15J = Convert.ToString(dtPCL.Rows[0]["F_QLTK_15J"]);
                                F_QLTK_GLF = Convert.ToString(dtPCL.Rows[0]["F_QLTK_GLF"]);
                            }
                            string UpPe = "";//更新sql
                            if (this.FormOperation.Operation.ToUpper() == "SUBMIT")//提交更新应收票据
                            {
                                //更新应收票据是否15加银行、是否关联票据
                                UpPe = string.Format(@"/*dialect*/  update T_CN_BILLRECEIVABLE set F_RDLV_CheckBox_qtr='{0}',F_RDLV_CheckBox_qtr_83g='{1}' where FID='{2}'", F_QLTK_15J, F_QLTK_GLF,FID);
                                DBServiceHelper.Execute(this.Context, UpPe);
                            }
                        
                        }

                    }

                }
            }
        }    
    }
}
